﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace EquipSecs300
{
    /// <summary>
    /// 点ini后缀的配置文件的读取帮助类
    /// </summary>
    public class IniHelper
    {
        static string strPath;
        static IniHelper()
        {
            //strPath = System.IO.Directory.GetCurrentDirectory();
            //strPath += "/JngServer/SecsConfig/EquipApp.ini";

            strPath = "./JngServer/SecsConfig/EquipAppData.ini";  //这里是相对路径
        }


        [DllImport("kernel32")]// 读配置文件方法的6个参数：所在的分区（section）、 键值、     初始缺省值、   StringBuilder、  参数长度上限 、配置文件路径
        public static extern long GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder retVal, int size, string filePath);
        [DllImport("kernel32")]//写入配置文件方法的4个参数：  所在的分区（section）、  键值、     参数值、       配置文件路径
        public static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
        /*读配置文件*/
        public static string GetValue(string section, string key)
        {

            try
            {
                if (File.Exists(strPath))  //检查是否有配置文件，并且配置文件内是否有相关数据。
                {
                    StringBuilder sb = new StringBuilder(255);
                    GetPrivateProfileString(section, key, "配置文件不存在，读取未成功!", sb, 255, strPath);
                    return sb.ToString();
                }
                else
                {
                    return string.Empty;
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        /*写配置文件*/
        public static void SetValue(string section, string key, string value)
        {

            try
            {
                if (!File.Exists(strPath))
                {
                    string filePath = Path.GetDirectoryName(strPath);
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    var fs = new FileStream(strPath, FileMode.OpenOrCreate);
                    fs.Close();
                }

                try
                {
                    WritePrivateProfileString(section, key, value, strPath);
                }
                catch (Exception ex)
                {

                    throw new Exception(ex.Message);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }



        }
    }
}
